$(document).ready(function(){

	"use strict";

	/* CONFIG ================================================================ */

	var config = {
		minHeaderHeight: 40,
		maxHeaderHeight: 80,
	}

	/* TEMPLATE ================================================================ */

	var template = {

		init: function(){
			/* init header */
			// this.header.init();
			// this.header.mainNav.init();

			/* init footer */
			// this.footer.init();

			/* init features */
			for(var feature in this.features){
				this.features[feature].init();
			}
		},

		/* HEADER -------------------------------- */

		header: {
			element: $("#header"),

			minHeight: 40,
			maxHeight: 80,

			init: function(){
				this.maxHeight = this.element.height();
				this.updateHeight();
				$(window).on("scroll", {t: this}, this.reqUpdateHeight);
			},

			updateHeight: function(){
				var v = Math.round(this.maxHeight - Math.max(0, Math.min(this.maxHeight - this.minHeight, $(window).scrollTop()*0.5 - 0)));

				/* handle header height */
				this.element.height(v);
			},

			reqUpdateHeight: function(e){
				e.data.t.updateHeight();
			},

			/* MAIN NAV ----- */
			
			mainNav: {
				element: $("#main-nav"),

				init: function(){
					this.element.find("a").each(function(){
						template.features.animatedScroll.build($(this));
					});
				}
			}

		},

		/* PAGE -------------------------------- */

		page: {
			element: $("#page"),
		},

		/* FOOTER -------------------------------- */

		footer: {
			element: $("#footer"),

			init: function(){
				$(window).on("scroll", {t: this}, this.reqUpdatePosition);
				this.updatePosition();
			},

			updatePosition: function(){
				var v = 0.5*($(window).scrollTop() - $(document).height() + $(window).height());
				if(Modernizr.csstransforms3d){
					this.element.css(helpers.prefix.css + "transform", "translate3d(0," + v + "px,0)");
				}else{
					this.element.css("top", v);
				}
			},

			reqUpdatePosition: function(e){
				e.data.t.updatePosition();
			}
		},

		/* FEATURES -------------------------------- */

		features: {
			
			/* CAROUSEL ----- */

			carousel: {
				init: function(){
					$(".carousel").owlCarousel({
						items : 3,
						navigation : true,
						slideSpeed: 500,
						scrollPerPage: true
					});
				}
			},
			
			/* BOOKMARK ----- */

			bookmark: {
				scrolling: false,

				init: function(){
					$(window)
					.on("mousewheel DOMMouseScroll", {t: this}, this.getBookmark)
					.on("keydown", {t: this}, this.getBookmark);
				},

				getBookmark: function(e){
					var t = e.data.t,
						f = false;

					if(!t.scrolling){
						if(e.keyCode == 40 || (e.originalEvent.deltaY || e.originalEvent.detail) > 0){
							$(".bookmark").each(function(){
								var pos = $(this).offset().top - $(window).scrollTop() - $(window).height(),
									a   = -$(window).height() + template.header.element.position().top + template.header.element.height() + 20,
									b   = 0;

								if(pos > a && pos < b){
									t.scrolling = true;
									template.helpers.scroller.scrollTo($(this), null, null, null, t.onScrollComplete);
									f = true;
									return false;
								}
							});

							if(f){
								e.preventDefault();
								return false;
							}
						}
					}else{
						e.preventDefault();
						return false;
					}
				},

				onScrollComplete: function(){
					template.features.bookmark.scrolling = false;
				}
			},
			
			/* SMOOTH SCROLL ----- */

			smoothScroll: {
				init: function(){
					$.smoothScroll();
				}
			},
			
			/* PARALLAX ----- */

			parallax: {
				init: function(){
					// $(".parallax").parallax();
				}
			},
			
			/* ANIMATED SCROLL ----- */

			animatedScroll: {
				init: function(){
					$(".animated-scroll").each(function(){
						template.features.animatedScroll.build($(this));
					});
				},

				build: function(el){
					$(el).click(function(){
						var t = $(this).data("animated-scroll-target") || $(this).attr("href") || "#";

						if(t == "#") t = 0;
						if(isNaN(t)) t = $(t);

						template.helpers.scroller.scrollTo(t, $(this).data("animated-scroll-duration"), $(this).data("animated-scroll-easing"));

						return false;
					});
				}
			},
			
			/* SCROLL SPY ----- */

			scrollSpy: {
				init: function(){
					$("body").scrollspy({
						target: template.header.mainNav.element.selector,
						offset: template.header.element.height() + 20
					});

					$(window).on("resize", {t: this}, this.handleResize);
				},

				handleResize: function(e){
					var t = e.data.t;

					clearTimeout(t._timeout);

					t._timeout = setTimeout(function(){
						$("body").data("bs.scrollspy").options.offset = template.header.element.height() + 20;
						$("body").scrollspy("refresh");
					});
				}
			},
			
			/* PAGE TOP LINK ----- */

			pageTopLink: {
				element: $("#page-top-link"),
				active: true,

				init: function(){
					template.features.animatedScroll.build(this.element);

					$(window).scroll(function(){
						template.features.pageTopLink.updateVisibility();
					});

					if($(window).scrollTop() < 150){
						this.element.css({
							"opacity": 0,
							"bottom": -35
						});
					}
				},

				updateVisibility: function(){
					if($(window).scrollTop() < 150){
						this.hide();
					}else{
						this.show();
					}
				},

				show: function(){
					if(this.active) return;
					this.active = true;
					TweenLite.to(this.element, 0.4, {opacity: 1, ease: Cubic.easeInOut});
					TweenLite.to(this.element, 0.4, {bottom: 0, ease: Cubic.easeInOut});
				},

				hide: function(){
					if(!this.active) return;
					this.active = false;
					TweenLite.to(this.element, 0.25, {opacity: 0, ease: Cubic.easeIn});
					TweenLite.to(this.element, 0.25, {bottom: -this.element.height(), ease: Cubic.easeIn});
				}
			}
		},

		/* HELPERS -------------------------------- */

		helpers: {

			/* SCROLLER ----- */

			scroller: {
				init: function(){
					
				},

				getOffset: function(){
					return -(template.header.element.position().top + template.header.element.height()); /* header is position-fixed */
				},

				scrollTo: function(t, duration, easing, update, complete){
					if($(t).length == 0 && t !== 0) return;
					
					var to = t instanceof jQuery ? t.offset().top : t;
					if(this.tween) this.tween.kill();
					this.tween = TweenLite.to($("body"), (duration*0.001 || 1), {scrollTo: {y: (to + this.getOffset()), autoKill: false}, ease: easing ? (typeof easing === "string" ? window[easing.split(".")[0]][easing.split(".")[1]] : easing) : Cubic.easeInOut, onUpdate: update, onComplete: complete});
				}
			}
		}

	}

	/* INIT TEMPLATE ================================================================ */

	template.init();

});